package com.company.bookstore.mapper.book_module;


import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.company.bookstore.entities.book_module.Book;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author 姬深威
 * @since 2020-09-12
 */
@Component
public interface BookMapper extends BaseMapper<Book> {

    //判断有无FROM TO 的 动态sql的判断条件，提取出来复用
    String FROM_TO = "<if test='from != null' > " +
            "AND price BETWEEN #{from} AND #{to} " +
            "</if> ";
    @Select("SELECT book_name FROM t_book")
    List<String> getNames();

    @Select("SELECT * FROM t_book WHERE id = #{bookId}")
    @Results(id = "bookMap", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "bookImgs", many = @Many(select = "com.company.bookstore.mapper.book_module.BookImgMapper.getBookImgByBookId"), column = "id"),
            @Result(property = "categories", many = @Many(select = "com.company.bookstore.mapper.book_module.CategoryMapper.getCategoryByBookId"), column = "id")
    })
    Book getBookById(Integer bookId);

    @Select("<script> " +
            "SELECT * FROM t_book " +
            "WHERE 1=1" + FROM_TO + "ORDER BY ${orderType} " +    //这里不能用#{}
            "</script>")   //用<script>标签包裹可以使用<if>标签实现动态sql
    @ResultMap("bookMap")
    IPage<Book> getAllBooks(IPage<Book> page, String orderType, Integer from, Integer to);

    @Select("<script> " +
            "SELECT * FROM t_book " +
            "WHERE 1=1 AND deleted=0 " + FROM_TO + "ORDER BY ${orderType} " +    //这里不能用#{}
            "</script>")   //用<script>标签包裹可以使用<if>标签实现动态sql
    @ResultMap("bookMap")
    IPage<Book> getAllBooksForFront(IPage<Book> page, String orderType, Integer from, Integer to);

    @Select("<script> " +
            "SELECT t_book.* FROM t_book,t_category,t_book_category " +
            "WHERE 1=1 " +
            "AND t_book_category.fk_category_id=t_category.id " +
            "AND t_book.id=t_book_category.fk_book_id " +
            "AND t_category.id=#{cId} " + FROM_TO +
            "ORDER BY ${orderType}" +
            " </script>")//这里不能用#{}
    @ResultMap("bookMap")
    IPage<Book> getBooksByCid(IPage<Book> page, Integer cId, String orderType, Integer from, Integer to);

    @Select("<script> " +
            "SELECT t_book.* FROM t_book,t_category,t_book_category " +
            "WHERE 1=1 AND deleted=0 " +
            "AND t_book_category.fk_category_id=t_category.id " +
            "AND t_book.id=t_book_category.fk_book_id " +
            "AND t_category.id=#{cId} " + FROM_TO +
            "ORDER BY ${orderType}" +
            " </script>")//这里不能用#{}
    @ResultMap("bookMap")
    IPage<Book> getBooksByCidForFront(IPage<Book> page, Integer cId, String orderType, Integer from, Integer to);

    @Select("<script> " +
            "SELECT * FROM t_book " +
            "WHERE 1=1 " +
            "AND book_name LIKE #{bookName} " + FROM_TO +
            "ORDER BY ${orderType1} " +
            "</script>")
    @ResultMap("bookMap")
    IPage<Book> getBooksByName(IPage<Book> page, String bookName, String orderType1, Integer from, Integer to);

    @Select("<script> " +
            "SELECT * FROM t_book " +
            "WHERE 1=1 AND deleted=0 " +
            "AND book_name LIKE #{bookName} " + FROM_TO +
            "ORDER BY ${orderType1} " +
            "</script>")
    @ResultMap("bookMap")
    IPage<Book> getBooksByNameForFront(IPage<Book> page, String bookName, String orderType1, Integer from, Integer to);

    @Select("<script> " +
            "SELECT * FROM t_book " +
            "WHERE 1=1 " +
            "AND author LIKE #{author} " + FROM_TO +
            "ORDER BY ${orderType1} " +
            "</script>")
    @ResultMap("bookMap")
    IPage<Book> getBooksByAuthor(IPage<Book> page, String author, String orderType1, Integer from, Integer to);

    @Select("<script> " +
            "SELECT * FROM t_book " +
            "WHERE 1=1 AND deleted=0 " +
            "AND author LIKE #{author} " + FROM_TO +
            "ORDER BY ${orderType1} " +
            "</script>")
    @ResultMap("bookMap")
    IPage<Book> getBooksByAuthorForFront(IPage<Book> page, String author, String orderType1, Integer from, Integer to);

    @Select("<script> " +
            "SELECT t_book.* FROM t_book,t_category,t_book_category " +
            "WHERE 1=1 " +
            "AND t_category.id=t_book_category.fk_category_id " +
            "AND t_book_category.fk_book_id=t_book.id " +
            "AND t_category.name LIKE #{categoryName} " + FROM_TO +
            "ORDER BY ${orderType1} " +
            "</script>")
    @ResultMap("bookMap")
    IPage<Book> getBooksByCategoryName(IPage<Book> page, String categoryName, String orderType1, Integer from, Integer to);

    @Select("<script> " +
            "SELECT t_book.* FROM t_book,t_category,t_book_category " +
            "WHERE 1=1 AND deleted=0 " +
            "AND t_category.id=t_book_category.fk_category_id " +
            "AND t_book_category.fk_book_id=t_book.id " +
            "AND t_category.name LIKE #{categoryName} " + FROM_TO +
            "ORDER BY ${orderType1} " +
            "</script>")
    @ResultMap("bookMap")
    IPage<Book> getBooksByCategoryNameForFront(IPage<Book> page, String categoryName, String orderType1, Integer from, Integer to);

    @Select("<script> " +
            "SELECT * FROM t_book " +
            "WHERE 1=1 " + FROM_TO +
            "ORDER BY sales DESC " +
            "</script>")
    @ResultMap("bookMap")
    IPage<Book> getHotSalesBooks(IPage<Book> page, Integer from, Integer to);


    @Insert("INSERT INTO t_book (book_name,repository_num,book_des,author,publisher,price,old_price) " +
            "VALUES (#{bookName},#{repositoryNum},#{bookDes},#{author},#{publisher},#{price},#{oldPrice});")
    @SelectKey(statement = "SELECT LAST_INSERT_ID()", keyColumn = "id", keyProperty = "id", resultType = Integer.class, before = false)
//返回上一条插入的id
    Integer addBooks(Book book);

    @Delete("DELETE FROM t_book WHERE id=#{bookId};")
    Integer deleteBook(Integer bookId);

    @Delete("<script> " +
            "DELETE FROM t_book WHERE id in " +
            "<foreach collection='array' open='(' close=')' item='item' separator=','> " +
            "#{item} " +
            "</foreach>" +
            " </script>")
    Integer deleteSelectedBooks(Integer[] selection);

    @Update("UPDATE t_book SET " +
            "book_name=#{bookName},repository_num=#{repositoryNum},book_des=#{bookDes}," +
            "author=#{author},publisher=#{publisher},price=#{price} " +
            "WHERE id=#{id}")
    Integer updateBook(Book book);

    @Update("<script>" +
            "UPDATE t_book SET " +
            "deleted=#{isdeleted} " +
            "WHERE id IN <foreach collection='selection' item='id' open='(' close=')' separator=','> " +
            "#{id} " +
            "</foreach>" +
            "</script>")
    Integer groundingBooks(Integer isdeleted,@Param("selection") Integer[] selection);

    /**
     * 增加销量
     *
     * @param bookId
     * @param salesNum
     * @return
     */
    @Update("UPDATE t_book SET sales=sales+#{salesNum} WHERE id=#{bookId}")
    Integer increaseSales(Integer bookId, Integer salesNum);
/*
    @Update("UPDATE t_book SET hot_click=hot_click+#{clickNum} WHERE id=#{bookId}")
    int increaseHotClick(int bookId,int clickNum);
*/

    @Update("UPDATE t_book SET deleted=#{deleted} WHERE id=#{id}")
    Integer handleGrounding(Book book);
}
